<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close :close-on-press-escape="mode != 'show' ? false : true" @closed="$emit('closed')" :close-on-click-modal="false" align-center>
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" label-width="100" ref="dialogForm" label-position="top">
            <el-form-item label="应付款" prop="">
                <el-input v-model="balance_money" placeholder="应付款" readonly></el-input>
            </el-form-item>
            <el-form-item label="本次结算" prop="discount_money">
                <el-input v-model="form.discount_money" placeholder="本次结算" clearable @input="disInput"></el-input>
            </el-form-item>
            <el-form-item label="折扣金额" prop="discount_money">
                <el-input v-model="form.discount_money" placeholder="折扣金额" clearable @input="disInput"></el-input>
            </el-form-item>
            <el-form-item label="实付金额" prop="discount_money">
                <el-input v-model="form.discount_money" placeholder="实付金额" clearable @input="disInput"></el-input>
            </el-form-item>
            <el-form-item label="对应账户" prop="discount_money">
                <el-select v-model="form.settlement_id" placeholder="请选择" filterable>
                    <el-option v-for="item in accountList" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="付款日期" prop="business_date">
                <el-date-picker v-model="form.business_date" type="datetime" placeholder="请选择付款日期" style="width: 100%;" :value-format="'YYYY-MM-DD HH:mm:ss'"></el-date-picker>
            </el-form-item>
            <el-form-item label="照片" prop="enclosure">
                <sc-upload height="80" width="80" v-model="form.enclosure"></sc-upload>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="备注" type="textarea"></el-input>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit(0)">保 存</el-button>
		</template>
	</el-dialog>
</template>
<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '付款',
				edit: '付款',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			form: {
				id: null,
				business_date: this.moment().format('YYYY-MM-DD HH:mm:ss'),
			},
            balance_money: 0,
			//验证规则
			rules: {
				supplier_id: [{ required: true, message: '请选择供应商' }],
			},
            accountList: [],
		}
	},
	mounted() {
        this.getAccount();
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.form.id) {
						res = await this.$API.finance.fuorder.edit.post(this.form);
					} else {
						res = await this.$API.finance.fuorder.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				}
			})
		},
		//表单注入数据
		async setData(data) {
			this.form.id = data.id
		},
        // 获取账户
        async getAccount() {
            var res = await this.$API.basic.account.all.get();
            const array = []
            res.data.map(item=>{
                array.push({
                    id: item.id,
                    name: `${item.account}【${item.paytype?.paytype_name}】余额: ${item.balance}`
                })
            })
            this.accountList = array
        },
	}
}
</script>

<style></style>
